package com.since.data_structure.binarytree;

/**
 * Created by Sincerity on 2020/5/20.
 * 描述：数组转为二叉树或者顺序存储二叉树
 */
class ArrBinaryTree {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        //创建一个 ArrBinaryTree
        OrderBinaryTree arrBinaryTree = new OrderBinaryTree(arr);
        arrBinaryTree.preOrder();
    }
}

class OrderBinaryTree {
    int[] array;

    public OrderBinaryTree(int[] array) {
        this.array = array;
    }

    public void preOrder() {
        this.preOrder(0);
    }

    public void preOrder(int index) {
        if (array == null || array.length == 0) {
            return;
        }
        System.out.println(array[index]);
        //左边遍历
        if ((index * 2 + 1) < array.length) {
            preOrder(index * 2 + 1);
        }
        //左边遍历
        if ((index * 2 + 2) < array.length) {
            preOrder(index * 2 + 2);
        }
    }
}

